$(function(){
    var plugin = "brelator";
    var default_params = {
        api : "feeds/posts/summary/-/",
        target : "div#brelator",
        max_entry : 30,
        max_display : 10
    };
    $[plugin] = $.sub();
    $[plugin].fn.pluginit = function(params){
        $.extend(default_params, params, {});
    };

    var entry_labels = [];
    $[plugin].fn.append = function(label){
        entry_labels.push(label);
    }

    var target_blog;
    $[plugin].fn.load = function(url){
        target_blog = url.match(/http:\/\/[^\/]+\//);
        var result = [];
        if(entry_labels == 0){
            return;
        }else if(entry_labels.length > 1){
            get_multiple_label_entries(entry_labels, result);
        }else{
            get_single_label_entries(entry_labels, result);
        }
    };

    function get_entries_from_json(data, array){
        var entries = data.feed.entry;
        $(entries).each(function(i, entry){
            $(entry.link).each(function(j, link){
                if(link.rel == "alternate"){
                    if(i < default_params.max_entry){
                        array.push("<a href=\"" + link.href + "\">" + entry.title.$t + "</a>");
                    }
                }
            });
        });
    }

    function get_multiple_label_entries(labels, result){
        var query = "";
        $(labels).each(function(i, label){query += encodeURI(label) + "/";});
        query = query.substring(0, query.length - 1);
        query = target_blog + default_params.api + query + "/?alt=json";

        $.getJSON(query, function(data){
            get_entries_from_json(data, result);
            if(result.length < default_params.max_entry){
                get_single_label_entries(labels, result);
            }
            else{
                show(result);
            }
        });
    }

    function get_single_label_entries(labels, result){
        if(labels.length < 1){
            show(result);
            return;
        }

        var label = encodeURI(labels.shift());
        var query = target_blog + default_params.api + label + "/?alt=json";

        $.getJSON(query, function(data){
            get_entries_from_json(data, result);
            get_single_label_entries(labels, result);
        });
    }

    function show(result){
        result = unique(result);
        result = shuffle(result);
        $(default_params.target).append("<ul>");
        $(result).each(function(i, value){
            if(i < default_params.max_display){
                $(default_params.target).append("<li>" + value + "</li>");
            }
        });
        $(default_params.target).append("</ul>");
    }

    function unique(array){
        var temporary = {}, value;
        var result = [];
        for(var i = 0; i < array.length; i++){
            value = array[i];
            if(!(value in temporary)){
                temporary[value] = true;
                result.push(value);
            }
        }
        return result;
    }

    function shuffle(array){
        var pool, target;
        var length = array.length;
        for(var i = 0; i < length * 10; i++){
            target = Math.floor(Math.random() * length);
            pool = array[target];
            array.splice(target, 1);
            array.push(pool);
            array.reverse();
        }
        return array;
    }
});